﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ccit.Utils
{
    /// <summary>
    /// 字符串数据类型扩展类
    /// </summary>
    public static class StringExt
    {
        /// <summary>
        /// 将数字金额转换成人民币大写格式
        /// </summary>
        /// <param name="sMoney"></param>
        /// <returns></returns>
        public static string ConvertToMoneyUCase(this string sMoney)
        {            
            Double numberValue = 0; // 数字金额
            if (sMoney != "")
                numberValue = Math.Round(Double.Parse(sMoney) * 100);
            string chineseValue = "";          // 转换后的汉字金额
            string String1 = "零壹贰叁肆伍陆柒捌玖";       // 汉字数字
            string String2 = "万仟佰拾亿仟佰拾万仟佰拾元角分";     // 对应单位
            int len = numberValue.ToString().Length;         // numberValue 的字符串长度
            string Ch1;             // 数字的汉语读法
            string Ch2;             // 数字位的汉字读法
            int nZero = 0;            // 用来计算连续的零值的个数
            int String3 = 0;            // 指定位置的数值
            if (len > 15)
            {

                return "";
            }
            if (numberValue == 0)
            {

                chineseValue = "零元整";
                return chineseValue;

            }

            String2 = String2.Substring(String2.Length - len, len);   // 取出对应位数的STRING2的值

            for (int i = 0; i < len; i++)
            {
                if (numberValue.ToString().Substring(i, 1) == "-")
                {
                    chineseValue = "负";
                    continue;
                }
                String3 = int.Parse(numberValue.ToString().Substring(i, 1));   // 取出需转换的某一位的值
                //alert(String3);
                if (i != (len - 3) && i != (len - 7) && i != (len - 11) && i != (len - 15))
                {

                    if (String3 == 0)
                    {

                        Ch1 = "";
                        Ch2 = "";
                        nZero = nZero + 1;

                    }
                    else if (String3 != 0 && nZero != 0)
                    {

                        Ch1 = "零" + String1.Substring(String3, 1);
                        Ch2 = String2.Substring(i, 1);
                        nZero = 0;

                    }
                    else
                    {

                        Ch1 = String1.Substring(String3, 1);
                        Ch2 = String2.Substring(i, 1);
                        nZero = 0;
                    }
                }
                else
                {              // 该位是万亿，亿，万，元位等关键位
                    if (String3 != 0 && nZero != 0)
                    {

                        Ch1 = "零" + String1.Substring(String3, 1);
                        Ch2 = String2.Substring(i, 1);
                        nZero = 0;

                    }
                    else if (String3 != 0 && nZero == 0)
                    {

                        Ch1 = String1.Substring(String3, 1);
                        Ch2 = String2.Substring(i, 1);
                        nZero = 0;

                    }
                    else if (String3 == 0 && nZero >= 3)
                    {

                        Ch1 = "";
                        Ch2 = "";
                        nZero = nZero + 1;

                    }
                    else
                    {

                        Ch1 = "";
                        Ch2 = String2.Substring(i, 1);
                        nZero = nZero + 1;

                    }

                    if (i == (len - 11) || i == (len - 3))
                    {    // 如果该位是亿位或元位，则必须写上
                        Ch2 = String2.Substring(i, 1);
                    }

                }
                chineseValue = chineseValue + Ch1 + Ch2;

            }

            if (String3 == 0)
            {           // 最后一位（分）为0时，加上“整”
                chineseValue = chineseValue + "整";
            }
            return chineseValue;
        }
        
    }
}
